/*@author cerimice*/
package Persistencia.Fachada;

import Persistencia.Definicion.TipoServicio;

import java.util.logging.Logger;
import javax.persistence.Persistence;
import javax.persistence.EntityManager;

public class TipoServicioFachada
    {
     private EntityManager em;
    
    public TipoServicioFachada()
        {}
    
    private EntityManager getEntityManager()
        {return Persistence.createEntityManagerFactory(Sistema.Configuracion.getUnidadDePersistencia()).createEntityManager();}                
    
    public boolean agregar(String nombre) throws Exception
        {
        try
            {
            em = getEntityManager();
            em.getTransaction().begin();
                TipoServicio obj = new TipoServicio();
                obj.setNombre(nombre.toUpperCase());
            em.persist(obj);
            em.getTransaction().commit();
            em.close();
            return true;
            }
        catch(Exception Ex)
            {Logger.getLogger(this.getClass().getName()).log(Sistema.Configuracion.nivelLoggin(),Ex.getMessage()); throw Ex;}
        }
    
    public boolean modificar(String clave,String nombre) throws Exception
        {
        try
            {
            em = getEntityManager();
            em.getTransaction().begin();
                TipoServicio obj = em.find(TipoServicio.class,clave);
                if(obj == null){return false;}
                obj.setNombre(nombre.toUpperCase());
            em.persist(obj);
            em.getTransaction().commit();
            em.close();
            return true;
            }
        catch(Exception Ex)
            {Logger.getLogger(this.getClass().getName()).log(Sistema.Configuracion.nivelLoggin(),Ex.getMessage()); throw Ex;}
        }
    
    public TipoServicio obtener(String clave) throws Exception
        {
        try
            {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoServicio obj = em.find(TipoServicio.class,clave);
            em.getTransaction().commit();
            em.close();
            return obj;
            }
        catch(Exception Ex)
            {Logger.getLogger(this.getClass().getName()).log(Sistema.Configuracion.nivelLoggin(),Ex.getMessage()); throw Ex;}
        }    
    }
